package com.guomei.controller.curse;

import com.guomei.bean.curse.CourseCategoryRelation;
import com.guomei.bean.curse.CreateOrUpdateCourseCategoryRelationDTO;
import com.guomei.service.curse.CourseCategoryRelationService;
import com.guomei.utils.Result;
import com.guomei.utils.ValidationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 课程与类别关联表控制器
 */
@RestController
@RequestMapping("/api1/course-category-relation")
@Slf4j
public class CourseCategoryRelationController {
    @Resource
    private CourseCategoryRelationService relationService;

    /**
     * 新增/修改课程类别关联
     */
    @PostMapping("/addRelation")
    public Result<?> addRelation(@Valid @RequestBody CreateOrUpdateCourseCategoryRelationDTO createOrUpdateCourseCategoryRelationDTO, BindingResult result) {
        log.info("新增/修改类别关联：{}", createOrUpdateCourseCategoryRelationDTO);
        List<CourseCategoryRelation> relations = new ArrayList<>();
        //实体类注解参数校验
        Result commonDataResponse = ValidationUtil.validInputParams(result);
        if (commonDataResponse != null) {
            return commonDataResponse;
        }
        try {
            if (createOrUpdateCourseCategoryRelationDTO.getId() != null){
                int i = relationService.updateCourseRelations(createOrUpdateCourseCategoryRelationDTO);
                return i > 0 ? Result.success("更新成功") : Result.fail("更新失败");
            } else {
                for (Long id : createOrUpdateCourseCategoryRelationDTO.getCourseIds()){
                    CourseCategoryRelation relation = new CourseCategoryRelation();
                    relation.setCourseId(id);
                    relation.setCategoryId(createOrUpdateCourseCategoryRelationDTO.getCategoryId());
                    relation.setCreatedTime(LocalDateTime.now());
                    relations.add(relation);
                }
                int i = relationService.batchAddRelations(relations);
                return i > 0 ? Result.success("新增成功") : Result.fail("新增失败");
            }
        } catch (Exception e) {
            log.info("新增类别关联失败");
            return Result.fail("新增类别关联失败");
        }
    }

    /**
     * 根据条件查询类别关联信息
     */
    @PostMapping("/selectByCondition")
    public Result<?> selectByCondition(@RequestBody CourseCategoryRelation courseCategory) {
        try {
            if (courseCategory == null || courseCategory.getPageNum() == null || courseCategory.getPageSize() == null){
                return Result.fail("分页字段信息缺失，查询失败");
            }
            // 验证分页参数是否有效
            if (courseCategory.getPageNum() <= 0 || courseCategory.getPageSize() <= 0) {
                return Result.fail("分页参数无效");
            }
            return relationService.selectByCondition(courseCategory);
        } catch (Exception e) {
            log.info("根据条件查询类别关联信息失败");
            return Result.fail("根据条件查询类别关联信息失败");
        }
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Result<?> delete(@RequestBody CourseCategoryRelation courseCategory) {
        try {
            log.info("根据条件查询类别关联信息：{}", courseCategory);
            if (courseCategory == null || courseCategory.getCategoryId() == null){
                return Result.fail("类别关联ID信息缺失，删除失败");
            }
            int i = relationService.deleteRelationByCourseIdAndCategoryId(courseCategory.getCourseId(), courseCategory.getCategoryId());
            return i > 0 ? Result.success("删除成功") : Result.fail("删除失败");
        } catch (Exception e) {
            log.info("根据条件查询类别关联信息失败");
            return Result.fail("根据条件查询类别关联信息失败");
        }
    }
}
